﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SharpCmd.Controls.Viewers
{
    /// <summary>
    /// ViewerForm - a dialog for viewing files
    /// </summary>
    internal partial class ViewerForm : Form
    {
        private ViewerType viewerType;
        private ViewerControl viewerControl;

        /// <summary>
        /// Creates new instance of ViewerForm
        /// </summary>
        public ViewerForm()
        {
            InitializeComponent();
            //TODO load viewer default settings
            viewerType = ViewerType.Text;
        }

        public string SelectedFile { get; set; }

        /// <summary>
        /// Changes the viewer type
        /// </summary>
        public ViewerType ViewerType
        {
            get
            {
                return viewerType;
            }
            set
            {
                viewerType = value;
            }
        }

        public void ShowViewer()
        {
            //load viewer
            viewerControl = ViewerControl.Get(viewerType);
            viewerControl.FileName = SelectedFile;
            viewerControl.PercentChanged += new EventHandler(viewerControl_PercentChanged);
            viewerControl.Initialize();
            this.panelContent.Controls.Clear();
            this.panelContent.Controls.Add(viewerControl);
            this.Text = string.Format("Viewer - {0}", SelectedFile);
            viewerControl_PercentChanged(viewerControl, null);
            viewerControl.Display();
        }

        void viewerControl_PercentChanged(object sender, EventArgs e)
        {
            statusToolStripMenuItem.Text = string.Format("{0:0.0}%", viewerControl.Percent);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (msg.Msg)
            {
                case Windows.Events.WM_KEYDOWN:
                    switch ((Keys)msg.WParam.ToInt32())
                    {
                        case Keys.Up:
                        case Keys.Down:
                        case Keys.PageUp:
                        case Keys.PageDown:
                        case Keys.Home:
                        case Keys.End:
                            viewerControl.OnKeyDown((Keys)msg.WParam.ToInt32());
                            return true;
                        case Keys.Escape:
                            Close();
                            return true;
                    }
                    break;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void wrapTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void aNSIDOSToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void aSCIIWindowsToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void nonfixedWidthToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void ViewerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (viewerControl != null)
                viewerControl.Uninitialize();
        }
    }
}